#!/bin/bash
###
# @Author: Loganli
# @Date: 2025-05-08
# @Description: Loki服务快速启动脚本
###

# 确保在loki目录下执行
if [ "$(basename $(pwd))" != "loki" ]; then
  echo "请在loki目录下执行此脚本"
  exit 1
fi

# 创建必要的目录
mkdir -p data/wal data/index data/boltdb-cache data/chunks data/compactor
# 设置目录权限
chmod -R 777 data

# 检查是否存在.env文件，如果不存在则创建
if [ ! -f .env ]; then
  echo "创建.env文件，请根据需要修改AWS凭证"
  echo "AWS_ACCESS_KEY_ID=your-access-key-id" > .env
  echo "AWS_SECRET_ACCESS_KEY=your-secret-access-key" >> .env
fi

# 启动服务
docker-compose up -d

# 检查服务状态
echo "服务启动中，请稍候..."
sleep 5
docker-compose ps

# 检查Loki是否就绪
echo "检查Loki服务状态..."
curl -s http://localhost:3100/ready || echo "Loki服务尚未就绪，请稍后再试"

echo "
=====================================================
Loki 日志聚合服务已部署完成!
- Loki API: http://localhost:3100

查看日志:
  docker-compose logs loki

停止服务:
  docker-compose down
====================================================="
